package Basepractice.P2017_11_1;

import static Basepractice.P2017_11_1.For_while_dowhile.sum1;
import static Basepractice.P2017_11_1.For_while_dowhile.sum2;
import static Basepractice.P2017_11_1.For_while_dowhile.sum3;

/**
 * Created by Administrator on 2017/11/4.
 */
//分别用for语句、while语句、do while语句编写程序：求1至n之间能被m整除的整数之和
public class Refof_while_dowhile {
    public static void main(String[] args) {
        int n=10;
        int m=5;
        System.out.println("for 语句sum1="+sum1(n,m));
        System.out.println("while 语句sum2="+sum2(n,m));
        System.out.println("do while 语句sum3="+sum3(n,m));
    }
    public static long sum1(int n,int m) {
        int i = 0;
        long sum = 0;
        for (i = 1; i <= n; i++) {
            if (i % m == 0) {
                sum = sum + i;
            }

        } return sum;
    }
    public static long sum2(int n,int m) {
        int i=0;
        long sum=0;


        while (i<=n){
            if (i%m==0){
                sum=sum+i;
            }i++;
        }return sum;
    }
    public static long sum3(int n,int m) {
        int i=0;
        long sum=0;
        do {
            if (i%m==0){
                sum=sum+i;
            }i++;
        }while (i<=n);
     return sum;
    }
}
